<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- table表格区域 -->
      <el-table :data="goodslist" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_title"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="150px"></el-table-column>

        <el-table-column label="状态" prop="on_sale" width="150px">
          <template slot-scope="scope">
            <el-popconfirm title="您确定要执行此操作吗？" @onConfirm="status(scope.row)">
              <!-- <el-button slot="reference">删除</el-button> -->
              <el-button slot="reference" type="success" v-if="scope.row.on_sale==1">上架</el-button>
              <el-button slot="reference" type="danger" v-else>下架</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>

        <el-table-column label="是否推荐" prop="propaganda" width="150px">
          <template slot-scope="scope">
            <el-popconfirm title="您确定要执行此操作吗？" @onConfirm="propaganda(scope.row)">
              <!-- <el-button slot="reference">删除</el-button> -->
              <el-button slot="reference" type="primary" v-if="scope.row.propaganda==1">是</el-button>
              <el-button slot="reference" type="warning" v-else>否</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>

        <el-table-column label="发布时间" prop="create_time" width="220px">
          <template slot-scope="scope">{{scope.row.create_time|dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="240px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row.id)">编辑</el-button>

            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="remove(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1,2,5,10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
import { server } from '../api/index'
export default {
  data() {
    return {
      //查询对象
      queryInfo: {
        query: '',
        //当前页数
        pagenum: 1,
        //当前页显示多少条

        pagesize: 10
      },
      //商品列表
      goodslist: [],
      //条数
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    //根据分页获取对应的商品列表
    async getGoodsList() {
      const { data: res } = await server.getGoodsList(this.queryInfo)
      if (res.code !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.$message.success('获取商品列表成功')
      this.goodslist = res.data.data
      this.total = res.data.total
    },
    //监听分页改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //监听页码值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    //触发删除按钮
    remove(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },

    status(id) {
      this.$confirm('此操作将修改商品状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // this.delete(id)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    //执行删除操作
    async delete(id) {
      const { data: res } = await server.delGoods(id)

      if (res.code !== 200) {
        return this.$message.error('删除商品信息失败！')
      }
      //刷新列表
      this.getGoodsList()
      this.$message.success('删除商品信息成功！')
    },
    //添加商品
    goAddpage() {
      this.$router.push('/goods/add')
    },
    edit(id) {
      this.$router.push('/goods/edit/' + id)
    },
    async status(row) {
      if (row.on_sale == 1) {
        var on_sale = 0
      } else {
        var on_sale = 1
      }
      const { data: res } = await server.statusGoods(row.id, on_sale)
      if (res.code !== 200) {
        return this.$message.error('修改失败')
      }
      this.getGoodsList()
      this.$message.success('修改成功')
    },
    async propaganda(row) {
      if (row.propaganda == 1) {
        var propaganda = 0
      } else {
        var propaganda = 1
      }
      const { data: res } = await server.pstatusGoods(row.id, propaganda)
      if (res.code !== 200) {
        return this.$message.error('修改失败')
      }
      this.getGoodsList()
      this.$message.success('修改成功')
    }
  }
}
</script>
<style scoped>
</style>